/*
 * @lc app=leetcode.cn id=1218 lang=javascript
 *
 * [1218] 最长定差子序列
 */

// @lc code=start
/**
 * @param {number[]} arr
 * @param {number} difference
 * @return {number}
 */
var longestSubsequence = function (arr, difference) {
  const lengths = new Map();
  let max = 1;
  for (let i = 0; i < arr.length; i++) {
    if (lengths.has(arr[i] - difference)) {
      lengths.set(arr[i], lengths.get(arr[i] - difference) + 1);
      max = Math.max(lengths.get(arr[i]), max);
    } else {
      lengths.set(arr[i], 1);
    }
  }
  return max;
};
// @lc code=end
